package com.fc.v2.service;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.fc.v2.mapper.auto.SysDepartmentMapper;
import com.fc.v2.model.auto.SysDepartment;
import com.fc.v2.model.auto.TsysUser;
import com.fc.v2.model.custom.wcCount;
import com.fc.v2.shiro.util.ShiroUtils;
import com.fc.v2.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.v2.common.base.BaseService;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.mapper.auto.Wcbb2Mapper;
import com.fc.v2.model.auto.Wcbb2;
import com.fc.v2.model.auto.Wcbb2Example;
import com.fc.v2.model.custom.Tablepar;
import org.springframework.util.unit.DataUnit;

/**
 * Wcbb2Service
 *
 * @author zhangwei_自动生成
 * @Title: Wcbb2Service.java 
 * @Package com.fc.v2.service 
 * @email ${email}
 * @date 2022-02-03 10:41:02  
 **/
@Service
public class Wcbb2Service implements BaseService<Wcbb2, Wcbb2Example> {
    @Autowired
    private Wcbb2Mapper wcbb2Mapper;

    @Autowired
    private SysDepartmentMapper sysDepartmentMapper;

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Wcbb2> list(Tablepar tablepar, Wcbb2 wcbb2) {
        Wcbb2Example testExample = new Wcbb2Example();
        testExample.setOrderByClause("id desc");
        //搜索
        if (tablepar.getTypeName() == null) {
            tablepar.setTypeName("");
        }
        if (StrUtil.isNotEmpty(tablepar.getTypeName())) {
            switch (tablepar.getTypeName()) {
                case "bm":
                    wcbb2.setBm(tablepar.getSearchText());
                    break;
                case "xm":
                    wcbb2.setXm(tablepar.getSearchText());
                    break;
                case "zw":
                    wcbb2.setZw(tablepar.getSearchText());
                    break;
                case "ymjzqk":
                    wcbb2.setYmjzqk(tablepar.getSearchText());
                    break;
                case "cbsj":
                    wcbb2.setCbsj(DateUtils.parseDate(tablepar.getSearchText()));
                default:
                    break;
            }
            if (StrUtil.isNotEmpty(tablepar.getStartDate())) {
                testExample.createCriteria().andLikeQuery4(wcbb2, tablepar.getStartDate(), tablepar.getEndDate());
            } else {
                testExample.createCriteria().andLikeQuery(wcbb2);
            }

        } else {
            if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
                if (StrUtil.isNotEmpty(tablepar.getStartDate()) || StrUtil.isNotEmpty(tablepar.getEndDate())) {
                    testExample.createCriteria().andLikeQuery3(tablepar.getSearchText(), tablepar.getStartDate(), tablepar.getEndDate());
                } else {
                    testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
                }

            } else if (StrUtil.isNotEmpty(tablepar.getStartDate()) || StrUtil.isNotEmpty(tablepar.getEndDate())) {
                testExample.createCriteria().andLikeQueryDate(tablepar.getStartDate(), tablepar.getEndDate());
            } else {

                testExample.createCriteria().andLikeQuery(wcbb2);
            }

        }
        //获取用户信息
        TsysUser tsysUser = ShiroUtils.getUser();
        //根据单位查询
        if (tsysUser != null && tsysUser.getDepId().intValue() != 1) {
            SysDepartment sysDepartment = sysDepartmentMapper.selectByPrimaryKey(tsysUser.getDepId());
            testExample.or().andBmEqualTo(sysDepartment.getDeptName());
        }

        //表格排序
        //if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
        //}else{
        //	testExample.setOrderByClause("id ASC");
        //}
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<Wcbb2> list = wcbb2Mapper.selectByExample(testExample);
        PageInfo<Wcbb2> pageInfo = new PageInfo<Wcbb2>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {

        Integer[] integers = ConvertUtil.toIntArray(",", ids);
        List<Integer> stringB = Arrays.asList(integers);
        Wcbb2Example example = new Wcbb2Example();
        example.createCriteria().andIdIn(stringB);
        return wcbb2Mapper.deleteByExample(example);


    }


    @Override
    public Wcbb2 selectByPrimaryKey(String id) {

        Integer id1 = Integer.valueOf(id);
        return wcbb2Mapper.selectByPrimaryKey(id1);

    }


    @Override
    public int updateByPrimaryKeySelective(Wcbb2 record) {
        record.setUpdateTime(new Date());
        return wcbb2Mapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(Wcbb2 record) {

        record.setId(null);
        record.setCreateTime(new Date());

        return wcbb2Mapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(Wcbb2 record, Wcbb2Example example) {

        return wcbb2Mapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(Wcbb2 record, Wcbb2Example example) {

        return wcbb2Mapper.updateByExample(record, example);
    }

    @Override
    public List<Wcbb2> selectByExample(Wcbb2Example example) {

        return wcbb2Mapper.selectByExample(example);
    }


    @Override
    public long countByExample(Wcbb2Example example) {

        return wcbb2Mapper.countByExample(example);
    }


    @Override
    public int deleteByExample(Wcbb2Example example) {

        return wcbb2Mapper.deleteByExample(example);
    }

    /**
     * 修改权限状态展示或者不展示
     *
     * @param wcbb2
     * @return
     */
    public int updateVisible(Wcbb2 wcbb2) {
        return wcbb2Mapper.updateByPrimaryKeySelective(wcbb2);
    }

    public List<wcCount> selectYear(String year) {
        return wcbb2Mapper.selectYear(year);
    }

}
